﻿using Nova.Data;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nova_DAL
{
    public class TBase_InterfaceDal
    {
        DbHelper dbHelper = new DbHelper();

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Insert(string matCode)
        {
            string strSql = "INSERT INTO dbo.TBase_Interface(Mat_Code)VALUES(@matCode) SELECT @@IDENTITY";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSql.ToString());
            dbHelper.AddInParameter(cmd, "@matCode", DbType.String, matCode);
            object obj = dbHelper.ExecuteScalar(cmd);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                return 0;
            else
                return int.Parse(obj.ToString());
        }

        /// <summary>
        /// 根据物料编码查询二级编码
        /// </summary>
        /// <param name="matCode"></param>
        /// <returns></returns>
        public DataTable GetInfoByMatCode(string matCode)
        {
            string strSql = "SELECT Sec_Mat_ID,Mat_Code FROM dbo.TBase_Interface WHERE Mat_Code=@matCode";
            DbCommand cmd = dbHelper.GetSqlStringCommond(strSql.ToString());
            dbHelper.AddInParameter(cmd, "@matCode", DbType.String, matCode);
            DataTable dt = dbHelper.ExecuteDataTable(cmd);
            return dt;
        }

        public string GetSecMatId(string matCode,string ts01)
        {
            DbCommand cmd = dbHelper.GetStoredProcCommond("dbo.SP_Get_Sec_Mat_ID");
            dbHelper.AddInParameter(cmd, "@Mat_Code", DbType.String, matCode);
            dbHelper.AddInParameter(cmd, "@D01", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D02", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D03", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D04", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D05", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D06", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D07", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D08", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D09", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@D10", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S01", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S02", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S03", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S04", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S05", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S06", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S07", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S08", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S09", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S10", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S11", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S12", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S13", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S14", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S15", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S16", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S17", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S18", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S19", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@S20", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@N01", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N02", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N03", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N04", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N05", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N06", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N07", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N08", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N09", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@N10", DbType.Decimal, 0);
            dbHelper.AddInParameter(cmd, "@DynamicState01", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@DynamicState02", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@DynamicState03", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@DynamicState04", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@DynamicState05", DbType.Int16, 0);
            dbHelper.AddInParameter(cmd, "@TS01", DbType.String, ts01);
            dbHelper.AddInParameter(cmd, "@TS02", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@TS03", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@TS04", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@TS05", DbType.String, "");
            dbHelper.AddInParameter(cmd, "@ForceNew", DbType.Int16, 1); //如果不存在2级码，强迫创建新的
            dbHelper.AddInParameter(cmd, "@UserName", DbType.String, "");//单画面调试用，单画面调试不然Request.Cookies["vNovaUserNo"].Value.ToString())报错

            dbHelper.AddOutParameter(cmd, "@Sec_Mat_ID", DbType.Int32, 20);
            dbHelper.AddOutParameter(cmd, "@ResNO", DbType.Int16, 10);
            dbHelper.AddOutParameter(cmd, "@ResMsg", DbType.String, 255);
            dbHelper.ExecuteNonQuery(cmd);
            int iSecs = Convert.ToInt32(dbHelper.GetParameter(cmd, "@Sec_Mat_ID").Value.ToString());
            string strSecs = iSecs.ToString();
            return strSecs;
        }
    }
}
